import type from "./action-type";
import * as Api from "api/api";

export const setPageSize = payload => {
  return {
    type: type.SET_PAGE_SIZE,
    payload
  };
};

export const setPageNum = payload => {
  return {
    type: type.SET_PAGE_NUM,
    payload
  };
};

export const setTotalNum = payload => {
  return {
    type: type.SET_TOTAL_NUM,
    payload
  };
};

export const setList = payload => {
  return {
    type: type.SET_LIST,
    payload
  };
};

export const setName = payload => {
  return {
    type: type.SET_NAME,
    payload
  };
};

export const setLoading = payload => {
  return {
    type: type.SET_LOADING,
    payload
  };
};

export const setShowAdd = payload => {
  return {
    type: type.SET_SHOW_ADD,
    payload
  };
};

export const fetchList = ({ name, pageSize, pageNum }) => {
  return async f => {
    f(setLoading(true));
    const res = await Api.getPromotionPositionList({
      name,
      pageSize,
      pageNum
    });
    f(setLoading(false));

    if (res.status === 200) {
      let { data } = res;
      f(setList(data.msg.objectList.map(item => ({ ...item, key: item.id }))));
      f(setTotalNum(data.msg.totalNum));
    }
  };
};
